﻿using System.IO;
using System.Windows;
using System.Windows.Input;
using HandyControl.Controls;
using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using SoonCode.Core;
using SoonCode.FocusTest.Api;
using SoonCode.FocusTest.Events;
using SoonCode.FocusTest.Models;
using BindableBase = Prism.Mvvm.BindableBase;

namespace SoonCode.FocusTest.ViewModels
{
    class LoginViewModel : BindableBase
    {
        public string VerName { get; } = GlobalModel.verName;
        public string AppTitle { get; set; } = "专注力评测系统登录";
        public string Username { get; set; }
        public string Password { get; set; }
        public bool RememberPassword { get; set; } = false;
        public bool RememberUser { get; set; } = false;
        private string userData = FileHelper.GetCurrentDir() + "\\user.data";

        private ICommand? _loginCommad;
        private Visibility isLoading = Visibility.Hidden;
        private bool loginEnabled = true;

        public bool LoginEnabled
        {
            get { return loginEnabled; }
            set
            {
                loginEnabled = value;
                RaisePropertyChanged();
            }
        }

        public Visibility IsLoading
        {
            get { return isLoading; }
            set
            {
                isLoading = value;
                RaisePropertyChanged();
            }
        }

        public ICommand LoginCommand
        {
            get { return _loginCommad ?? (this._loginCommad = new DelegateCommand(() => DoLogin())); }
        }

        private IEventAggregator _eventAggregator;
        private DialogService _dialogService;

        public LoginViewModel(IEventAggregator eventAggregator, DialogService dialogService)
        {
            this._eventAggregator = eventAggregator;
            this._dialogService = dialogService;
            string userData = FileHelper.GetCurrentDir() + "\\user.data";
            if (FileHelper.Exists(userData))
            {
                string data = FileHelper.ReadTxt(userData);
                data = data.Base64Decode();
                string[] splist = data.Split(":");
                if (splist.Length > 1)
                {
                    Username = splist[0];
                    Password = splist[1];
                    RememberPassword = true;
                    RememberUser = true;
                }
                else if (splist.Length < 1)
                {
                    Username = splist[0];
                    RememberUser = true;
                }
            }
        }

        public async void DoLogin()
        {
            IsLoading = Visibility.Visible;
            LoginEnabled = false;
            UserInfo user = null;
            try
            {
                user = await API.Login(Username, Password, "v2" + DeviceHelper.GetHardwareId().ToMD5String());
                if (user != null)
                {
                    File.Delete(userData);
                    if (RememberUser)
                    {
                        FileHelper.WriteTxt((Username).Base64Encode(), userData);
                    }

                    if (RememberPassword)
                    {
                        FileHelper.WriteTxt((Username + ":" + Password).Base64Encode(), userData);
                    }

                    GlobalModel.Instance.UserInfo = user;
                    GlobalModel.Instance.OrgInfo = await API.GetCurrOrg();
                    if (GlobalModel.Instance.OrgInfo == null)
                        return;
                    GlobalModel.Instance.ClassesList = await API.GetClasses(GlobalModel.Instance.OrgInfo.id);
                    GlobalModel.AccountDir = @$"{FileHelper.GetCurrentDir()}{GlobalModel.Instance.UserInfo.Account}\";
                    this._eventAggregator.GetEvent<LoginEvent>().Publish(user);
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                LoginEnabled = true;
                IsLoading = Visibility.Hidden;
            }
        }
    }
}